package com.java2nb.novel.pay.controller;

import com.alibaba.fastjson.JSON;
import com.java2nb.novel.pay.dto.PayNotifyRequestDTO;
import com.java2nb.novel.pay.service.PayNotifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/notify")
@Api(tags = "支付结果通知接口")
@Slf4j
public class PayNotifyController {
    @Autowired
    private PayNotifyService payNotifyService;

    @PostMapping(value = "/pay", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiOperation("支付结果通知")
    public String payNotify(PayNotifyRequestDTO requestDTO) {
        log.info("接收支付结果通知请求：{}", JSON.toJSONString(requestDTO));
        try {
            // 处理通知逻辑
            payNotifyService.handlePayNotify(requestDTO);
            return "success"; // 处理成功，返回success
        } catch (Exception e) {
            log.error("处理支付结果通知失败", e);
            return "fail"; // 处理失败，网关会重试
        }
    }
}